package cn.wolfcode.service.impl;

import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.mapper.SystemDictionaryItemMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.service.SystemDictionaryItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemDictionaryItemServiceImpl implements SystemDictionaryItemService {

    @Autowired
    private SystemDictionaryItemMapper systemDictionaryItemMapper;


    @Override
    public void delete(Long id) {
        systemDictionaryItemMapper.deleteByPrimaryKey(id);
    }


    @Override
    public SystemDictionaryItem get(Long id) {
        return systemDictionaryItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SystemDictionaryItem> listAll() {
        return systemDictionaryItemMapper.selectAll();
    }

    @Override
    public PageInfo<SystemDictionaryItem> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize()); //对下一句sql进行自动分页
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<SystemDictionaryItem>(systemDictionaryItems);
    }

    /**
     * 根据sdId查询sdi
     *
     * @return
     */
    @Override
    public PageInfo<SystemDictionaryItem> selectBySdId(SystemDictionaryItemQueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemMapper.selectBySdId(qo);
        PageInfo<SystemDictionaryItem> pageInfo = new PageInfo<>(systemDictionaryItems);
        return pageInfo;
    }

    @Override
    public void saveOrUpdate(SystemDictionaryItem systemDictionaryItem) {
        if (systemDictionaryItem == null) {
            throw new RuntimeException("没有可保存的数据");
        }
        if (systemDictionaryItem.getSd().getId() == null) {
            throw new RuntimeException("请先选择字典目录");
        }
        if (systemDictionaryItem.getId() != null) {
            systemDictionaryItemMapper.updateByPrimaryKey(systemDictionaryItem);
        } else {
            systemDictionaryItemMapper.insert(systemDictionaryItem);
        }
    }

    /**
     * 根据目录sn查询明细列表
     *
     * @param sn
     * @return
     */
    @Override
    public List<SystemDictionaryItem> selectByItemSn(String sn) {
        return systemDictionaryItemMapper.selectByItemSn(sn);
    }
}
